package com.welike.shibo.controller.v2.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.welike.shibo.aspect.annotation.AutoLog;
import com.welike.shibo.eneity.Goods;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.enums.ResultEnum;
import com.welike.shibo.service.GoodsService;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/20 5:32 下午
 */
@Slf4j
@Api(value = "商品相关接口")
@RestController
@RequestMapping("/v2/api/shops")
public class ShopsController {

    @Resource
    private GoodsService goodsService;

    @AutoLog(operateState = "商品列表")
    @RequiresPermissions("goods:list")
    @GetMapping("/list")
    public JsonResult list(
            @ApiParam(name = "page", value = "页数", required = false)
            @RequestParam(value = "page", defaultValue = "1", required = false) int page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam(value = "pageSize", defaultValue = "10", required = false)int pageSize
    ) {
        IPage<Goods> iPage = goodsService.queryGoodsPage(page, pageSize);
        return ResultUtils.success("list", iPage);
    }

    @AutoLog(operateState = "搜索商品")
    @GetMapping("/search")
    public JsonResult search(
            @RequestParam(value = "goods") String goods
    ){
        if (Tools.isNull(goods)){
            return ResultUtils.error(ResultEnum.E_90003);
        }
        return goodsService.searchGoods(goods);
    }

    @AutoLog(operateState = "添加商品")
    @RequiresPermissions("goods:add")
    @PostMapping("/add")
    public JsonResult add(
            @RequestBody JSONObject requestJson
    ){
        return goodsService.addGoods(requestJson);
    }

}
